﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace EPS.Common
{
    internal class MemorySerializer
    {
        public byte[] ToSerializer(object obj)
        {
            XmlObjectSerializer serializer = new DataContractSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return ms.ToArray();
            }
        }

        public TEntity ToEntity<TEntity>(byte[] butes)
        {
            TEntity obj = Activator.CreateInstance<TEntity>();
            XmlObjectSerializer serializer = new DataContractSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream(butes))
            {
                return (TEntity)serializer.ReadObject(ms);
            }
        }
    }
}
